﻿#include "componentslist.h"
#include "ui_compenentlist.h"
#include <QDebug>
#include <QDirIterator>
#include <QLibrary>

void CompenentList::ScanTaiJiPlugins(QString pluginsdir)
{
    pluginsdir = QCoreApplication::applicationDirPath() + "/" + pluginsdir;
    //qDebug() << pluginsdir;
    QDirIterator it(pluginsdir,QDir::Files|QDir::Dirs|QDir::NoDotAndDotDot);	//遍历所有目录和文件
    while (it.hasNext())//存在
    {
        QString name = it.next();//读取
        QFileInfo info(name);
        if (info.isDir())//判断是目录
        {
            //将目录加入运行环境变量
            continue;
        }
        else
        {
            if (info.suffix() == "dll")
            {
                //qDebug() << name;
                QLibrary hdll(pluginsdir+ "/" +info.fileName().toLocal8Bit().data());
                if(hdll.load()){
                    GetComponentNameFunc namefunc = (GetComponentNameFunc)hdll.resolve("GetComponentName");
                    GetNewComponentFunc func = (GetNewComponentFunc)hdll.resolve("GetNewComponent");
                    GetComponentDescriptionFunc desfunc = (GetComponentDescriptionFunc)hdll.resolve("GetComponentDescription");
                    //qDebug() << namefunc << func << desfunc;
                    if(namefunc&&func&&desfunc){
                        //qDebug() << namefunc();
                        QString funcname = namefunc();
                        m_desMap[funcname] = desfunc();
                        ui->listWidget->addItem(funcname);
                    }
                }
            }
        }
    }
}

CompenentList::CompenentList(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CompenentList)
{
    ui->setupUi(this);
    this->setWindowTitle(QStringLiteral("插件使用帮助"));
    ScanTaiJiPlugins("TaiJiPlugins");
}

CompenentList::~CompenentList()
{
    delete ui;
}

void CompenentList::on_listWidget_itemClicked(QListWidgetItem *item)
{
    ui->textBrowser->setText(m_desMap[item->text()]);
}
